package com.moduleSys.roleService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moduleSys.entity.system.Role;
import com.moduleSys.entity.system.RoleMenu;
import com.moduleSys.entity.system.User;
import com.moduleSys.mapper.RoleMapper;
import com.moduleSys.mapper.RoleMenuMapper;
import com.moduleSys.mapper.UserRoleMapper;
import com.moduleSys.roleService.RoleService;
import com.moduleSys.utils.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by czw on 2017/7/22 0022.
 */
@Service
public class RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor={RuntimeException.class, Exception.class})
    public Role insert(Role role, String mId) {
        roleMapper.insert(role);
        if( !StringUtils.isEmpty(mId) ){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            String[] menuArr = mId.split(",");
            List<RoleMenu> menuIdList = new ArrayList<RoleMenu>();
            roleMenuMapper.deleteByRoleId(roleMenu.getRoleId());
            for ( String roleMenuId : menuArr){
                menuIdList.add(new RoleMenu(roleMenu.getRoleId(), Long.parseLong(roleMenuId)));
            }
            roleMenuMapper.insertRoleMenuForeach(menuIdList);
        }
        return role;
    }

    public JsonResult list(Integer pageSize,Integer currentPage) {
        PageHelper.startPage(currentPage,pageSize);
        List<Role> list = roleMapper.list();
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        JsonResult jr = new JsonResult();
        jr.setData(list);
        jr.setNumber(pageInfo.getTotal());
        return jr;
    }

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor={RuntimeException.class, Exception.class})
    public JsonResult deleteBatch(Long[] itemIds) {
        List<Long> roleIdList = new ArrayList<>();
        if( itemIds != null && itemIds.length > 0 ){
            for ( Long id : itemIds){
                roleIdList.add(id);
            }
            roleMapper.deleteBatch(roleIdList);
            roleMenuMapper.deleteBatchByRoleId(roleIdList);
            userRoleMapper.deleteBatchByRoleId(roleIdList);
            return new JsonResult(1,"删除成功",null);
        }else{
            return new JsonResult(0,"删除失败",null);
        }

    }

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor={RuntimeException.class, Exception.class})
    public JsonResult updateRole(Role role, String mId) {
        roleMapper.updateByPrimaryKey(role);
        roleMenuMapper.deleteByRoleId(role.getId());
        if( !StringUtils.isEmpty(mId) ){
            String[] menuArr = mId.split(",");
            List<RoleMenu> menuIdList = new ArrayList<RoleMenu>();
            for ( String roleMenuId : menuArr){
                menuIdList.add(new RoleMenu(role.getId(), Long.parseLong(roleMenuId)));
            }
            roleMenuMapper.insertRoleMenuForeach(menuIdList);
        }
        return new JsonResult(1,null,null);

    }

    public JsonResult getRoleList() {
        List<Role> list = roleMapper.list();
        JsonResult jr = new JsonResult();
        jr.setData(list);
        return jr;
    }
}
